<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
include 'inc/PAGE.php';
$p = new PAGE("");

$loginOk = $p->s()->getvar("loginOk");
$username = $p->s()->getvar("username");
$type = $p->s()->getvar("type");

if($loginOk==1  && strlen($username)>0 && ($type==ADMIN || $type==LIB)) {

    $register = $_POST['register'];
    /*
[0] => username         - check if available
[1] => password         - password check with passwordagain
[2] => passwordagain
[3] => name
[4] => email            - something @ something . registrar
[5] => details
    */
    for($i=0;$i<count($register);$i++) {
        $register[$i] = $p->t()->cleaner($register[$i]);
    }

    $errorexists = 0;
    $errorstring = "";
    $exists =$p->db()->gettoprow("SELECT COUNT(*) FROM users WHERE username='".$register[0]."'");

    if($exists[0]!="0") {
        $errorstring = $errorstring."&errors[]=Το όνομα χρήστη χρησιμοποιείται ήδη!<br>";
        $errorexists++;
    }else {
        $errorstring = $errorstring."&errors[]=";
    }

    if(strlen($register[0])<3 ||strlen($register[0])>10) {
        $errorstring = $errorstring."&errors[]=Το όνομα χρήστη πρέπει να είναι 3-10 χαρακτήρες!";
        $errorexists++;
    }else {
        $errorstring = $errorstring."&errors[]=";
    }

    if(strlen($register[1])<3 ||strlen($register[1])>10) {
        $errorstring = $errorstring."&errors[]=Το password πρέπει να είναι 3-10 χαρακτήρες!";
        $errorexists++;
    }else {
        $errorstring = $errorstring."&errors[]=";
    }

    if(strlen($register[2])<3 ||strlen($register[2])>10) {
        $errorstring = $errorstring."&errors[]=To password πρέπει να είναι 3-10 χαρακτήρες!<br>";
        $errorexists++;
    }else {
        $errorstring = $errorstring."&errors[]=";
    }

    if($register[1]!=$register[2]) {
        $errorstring = $errorstring."&errors[]=Δεν επαληθεύεται το password!";
        $errorexists++;
    }else {
        $errorstring = $errorstring."&errors[]=";
    }

    if(strlen($register[3])<5) {
        $errorstring = $errorstring."&errors[]=Συμπληρώστε ονοματεπώνυμο";
        $errorexists++;
    }else {
        $errorstring = $errorstring."&errors[]=";
    }

    if(strlen($register[4])<5) {
        $errorstring = $errorstring."&errors[]=Συμπληρώστε email";
        $errorexists++;
    }else {
        $errorstring = $errorstring."&errors[]=";
    }
    
    if($errorexists>0) {
        for($i=0;$i<count($register);$i++) {
            $values[$i]=$register[$i];
        }
        $values[1] = "";
        $values[2] = "";

        for($i=0;$i<count($values);$i++) {
            $val=$val."&values[]=".$values[$i];
        }
        header("Location: ./ze_user_librarian_add.php?".$errorstring.$val);
    }else {
        $sql = "INSERT INTO users (id, username, password, name, details, email, type) VALUES (null,'".$register[0]."','".md5($register[1])."','".$register[3]."','".$register[5]."','".$register[4]."','".$p->t()->canregister($type)."')";
        $insert = $p->db()->insert($sql);
        if($insert==1) {
            header("Location: ./ze_user_librarian_view.php");
        }
    }
}else {
    header("Location: ./index.php");
}
?>
<a href="index.php">index.php</a>